﻿#include "battle_view.h"
#include "ui_battle_view.h"
#include "qpainter.h"
#include "dialog/myhelper.h"
#include "Report/reportdataforsort.h"
#include "constant.h"
Battle_view::Battle_view(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Battle_view)
{
    ui->setupUi(this);

    initTableView_A();
    initTableView_B();
    initTableView_C();
    initTableView_D();
    initWaveViews();
//    this->showFullScreen();
    initServer();
    initSlots();
    ui->pushButton_save->setVisible(false);
    if(Constant::ShowFullScreen)
        this->showFullScreen();
}

Battle_view::~Battle_view()
{
    delete ui;
}
void Battle_view::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(rect(),QPixmap(":/images/share/selectmodel_page_bg2.png"),QRect());
}
void Battle_view::initWaveViews()
{
    abcd_Chart=cc.createLineChart_Battle_Lines();
    ui->widget_lntrend->setChart(abcd_Chart);
    ui->widget_lntrend->setRenderHint(QPainter::Antialiasing, true);

    abcdHist_BarChart=cc.createABCDHistBarChart();
    ui->widget_HistBar->setChart(abcdHist_BarChart);
    ui->widget_HistBar->setRenderHint(QPainter::Antialiasing,true);
}
void Battle_view::on_pushButton_exit_clicked()
{
    bool b=myHelper::ShowMessageBoxQuesion("确认要退出吗？");
    if(b){
        QJsonObject CommandInfo;
        CommandInfo.insert("fcode",809);//功能码（结束测量）
        CommandInfo.insert("icode",1);//信息码，具体训练项目
        CommandInfo.insert("msg","指令下达");
        QHashDataInfoIterator i = hash_data_info_A.begin();
        for(; i != hash_data_info_A.end(); ++i)
        {
            DATA_INFO data = i.value();
            emit command_toClient(data.socketDescriptor,CommandInfo);
        }
        i = hash_data_info_B.begin();
        for(; i != hash_data_info_B.end(); ++i)
        {
            DATA_INFO data = i.value();
            emit command_toClient(data.socketDescriptor,CommandInfo);
        }
        i = hash_data_info_C.begin();
        for(; i != hash_data_info_C.end(); ++i)
        {
            DATA_INFO data = i.value();
            emit command_toClient(data.socketDescriptor,CommandInfo);
        }

        i = hash_data_info_D.begin();
        for(; i != hash_data_info_D.end(); ++i)
        {
            DATA_INFO data = i.value();
            emit command_toClient(data.socketDescriptor,CommandInfo);
        }
        this->close();
    }

}
void Battle_view::getUserInfo(QHashUserTable A, QHashUserTable B, QHashUserTable C, QHashUserTable D ,QString TaskID,QString Type,QString Scene,int Time)
{
    this->TaskID=TaskID;
    this->Type=Type;
    this->Scene=Scene;
    this->NeedTime=Time;
    startTime=  QDateTime::currentDateTime();
    RefreshTableA();
    RefreshTableB();
    RefreshTableC();
    RefreshTableD();
    //更新四组的hash数据，每个hash绑定ip，用于后续上传数据更新
    QHashUserTableIterator i = A.begin();
    DATA_INFO data_Info;
    USER_BATTLE_DATA UBD;
    isAExit=false;
    for(; i != A.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();
        data_Info.IP=data.IP;
        UBD.sName=data.sName;
        UBD.IP=data.IP;
        UBD.score=0;
        m_userBattleHash_A.insert(data.IP,UBD);
        hash_data_info_A.insert(data.IP,data_Info);
        Group_Label.insert(data.IP,1);
        isAExit=true;
    }

    isBExit=false;
    i = B.begin();
    for(; i != B.end(); ++i)
    {
        isBExit=true;
        USER_TABLE_DATA data = i.value();
        data_Info.IP=data.IP;
        UBD.sName=data.sName;
        UBD.IP=data.IP;
        UBD.score=0;
        m_userBattleHash_B.insert(data.IP,UBD);
        hash_data_info_B.insert(data.IP,data_Info);
        Group_Label.insert(data.IP,2);
    }

    isCExit=false;
    i = C.begin();
    for(; i != C.end(); ++i)
    {
        isCExit=true;
        USER_TABLE_DATA data = i.value();
        data_Info.IP=data.IP;
        UBD.sName=data.sName;
        UBD.IP=data.IP;
        UBD.score=0;
        m_userBattleHash_C.insert(data.IP,UBD);
        hash_data_info_C.insert(data.IP,data_Info);
        Group_Label.insert(data.IP,3);
    }

    isDExit=false;
    i = D.begin();
    for(; i != D.end(); ++i)
    {
        isDExit=true;
        USER_TABLE_DATA data = i.value();
        data_Info.IP=data.IP;
        UBD.sName=data.sName;
        UBD.IP=data.IP;
        UBD.score=0;
        m_userBattleHash_D.insert(data.IP,UBD);
        hash_data_info_D.insert(data.IP,data_Info);
        Group_Label.insert(data.IP,4);
    }

}
void Battle_view::initTableView_A()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_A->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_A = new QStandardItemModel(this);
    ui->tableView_A->setModel(m_pTableModel_A);//设置model
    ui->tableView_A->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << tr("No.")<<  tr("姓名")<<tr("贡献度");
    m_pTableModel_A->setHorizontalHeaderLabels(headerList);

    //设置列宽
    ui->tableView_A->setColumnWidth(COLUMN_NO,50);
    ui->tableView_A->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_A->setColumnWidth(COLUMN_SCORE, 150);

}

void Battle_view::initTableView_B()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_B->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_B = new QStandardItemModel(this);
    ui->tableView_B->setModel(m_pTableModel_B);//设置model
    ui->tableView_B->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << tr("No.")<<  tr("姓名")<<tr("贡献度");
    m_pTableModel_B->setHorizontalHeaderLabels(headerList);
    //设置列宽
    ui->tableView_B->setColumnWidth(COLUMN_NO,50);
    ui->tableView_B->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_B->setColumnWidth(COLUMN_SCORE, 150);

}

void Battle_view::initTableView_C()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_C->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_C = new QStandardItemModel(this);
    ui->tableView_C->setModel(m_pTableModel_C);//设置model
    ui->tableView_C->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << tr("No.")<<  tr("姓名")<<tr("贡献度");
    m_pTableModel_C->setHorizontalHeaderLabels(headerList);
    //设置列宽
    ui->tableView_C->setColumnWidth(COLUMN_NO,50);
    ui->tableView_C->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_C->setColumnWidth(COLUMN_SCORE, 150);

}

void Battle_view::initTableView_D()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_D->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_D = new QStandardItemModel(this);
    ui->tableView_D->setModel(m_pTableModel_D);//设置model
    ui->tableView_D->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << tr("No.")<<  tr("姓名")<<tr("贡献度");
    m_pTableModel_D->setHorizontalHeaderLabels(headerList);
    //设置列宽
    ui->tableView_D->setColumnWidth(COLUMN_NO,50);
    ui->tableView_D->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_D->setColumnWidth(COLUMN_SCORE, 150);

}
void Battle_view::initServer()
{
    Server::instance();
    connect(this,SIGNAL(command_toClient(int,QJsonObject)),Server::instance(),SLOT(writeMsgToClient(int,QJsonObject)));

    connect(Server::getInstance(),SIGNAL(updateMsg(QJsonObject&)),this,SLOT(receivedMsg(QJsonObject&)));
    connect(Server::getInstance(),SIGNAL(updateClient(QString)),this,SLOT(addNewClient(QString)));
    connect(Server::getInstance(),SIGNAL(removeClient(QString)),this,SLOT(removeClient(QString)));
}
void Battle_view::initSlots()
{
    timer_UpdataChart=new QTimer(this);
    timer_UpdataChart->start(3000);
    connect(timer_UpdataChart, SIGNAL(timeout()), this, SLOT(update_a_Data()));
}
void Battle_view::addNewClient(QString address)
{
    qDebug()<<"Connect OK to: "<<address;
}

void Battle_view::removeClient(QString address)
{

}
bool Battle_view:: compareData(const ReportDataForSort &A1,const ReportDataForSort &A2)
{
    return A1.score>A2.score;
}
void Battle_view::receivedMsg(QJsonObject & json)
{
    if(json.contains("IP"))
    {
        QString cIP=json["IP"].toString();
        int groupNum=Group_Label[cIP];
        DATA_INFO data_Info;
        USER_BATTLE_DATA UBD;
        switch (groupNum) {
        case 1:
            data_Info=hash_data_info_A[cIP];
            UBD=m_userBattleHash_A[cIP];
            break;
        case 2:
            data_Info=hash_data_info_B[cIP];
            UBD=m_userBattleHash_B[cIP];
            break;
        case 3:
            data_Info=hash_data_info_C[cIP];
            UBD=m_userBattleHash_C[cIP];
            break;
        case 4:
            data_Info=hash_data_info_D[cIP];
            UBD=m_userBattleHash_D[cIP];
            break;
        default:
            break;
        }
        if(json.contains("socketDescriptor"))
            data_Info.socketDescriptor=json["socketDescriptor"].toInt();
        if(json.contains("fcode"))
        {
            if(json.contains("socketDescriptor"))
                data_Info.socketDescriptor=json["socketDescriptor"].toInt();
            int fcode=json["fcode"].toInt();
            int icode=json["icode"].toInt();

            if(fcode ==107)
            {
                //测量结束,将结果显示置空
                qDebug()<<"107-start";
                qDebug()<<"107-end";
            }

            if(fcode==105)
            {
                if(icode==0){
                    if(json.contains("userInfoBean"))
                    {
                        qDebug()<<"userInfoBean-start";

                        UserInfoBean uib= dataPro.Json2UserInfoBean(json["userInfoBean"].toObject());
                        data_Info.UserID=uib.UserID;
                        data_Info.UserName=uib.UserName;

                        qDebug()<<"userInfoBean-end";
                    }
                    if(json.contains("ppgRealTimeIndex"))
                    {
                        qDebug()<<"ppgRealTimeIndex-start";
                        RealTimeIndex rti= dataPro.Json2RealTimeIndex(json["ppgRealTimeIndex"].toObject());
                        data_Info.HR=rti.pr;
                        data_Info.Ox=rti.ox;

                        qDebug()<<"ppgRealTimeIndex-end";
                    }

                }
                if(icode==1)
                {
                    if(json.contains("ppgResults"))
                    {
                        qDebug()<<"ppgResults-start";
                        PsychologyIndex psy= dataPro.Json2PsychologyIndex(json["ppgResults"].toObject());
                        data_Info.Spress_List.append(psy.SPress);
                        data_Info.Fatigure=psy.Fatigue;
                        data_Info.Ansiety=psy.Motion;
                        data_Info.Depress=100-psy.Motion;
                        data_Info.Activity=psy.Activity;
                        data_Info.Ballance=psy.Balance;
                        data_Info.AntiPress=psy.AntiPress;
                        data_Info.rrValidLen=psy.rrvalid;
                        for(int i=0;i<28;i++)
                            data_Info.hrvindex[i]=psy.hrvindex[i];

                        for(int i=0;i<psy.rrvalid;i++)
                            data_Info.RR[i]=psy.RR.at(i);
                        if(psy.PhysicalMentalHealth>0)
                            data_Info.PhysicalMentalHealth=psy.PhysicalMentalHealth;
                        data_Info.SDP=psy.sdp;
                        UBD.score=(int)psy.PhysicalMentalHealth;
                        qDebug()<<"ppgResults-end";
                    }
                }

            }

        }
        switch (groupNum) {
        case 1:
            hash_data_info_A.insert(cIP,data_Info);
            m_userBattleHash_A.insert(cIP,UBD);
            RefreshTableA();
            ui->tableView_A->sortByColumn(COLUMN_SCORE,Qt::DescendingOrder);
            break;
        case 2:
            hash_data_info_B.insert(cIP,data_Info);
            m_userBattleHash_B.insert(cIP,UBD);
            RefreshTableB();
            ui->tableView_B->sortByColumn(COLUMN_SCORE,Qt::DescendingOrder);
            break;
        case 3:
            hash_data_info_C.insert(cIP,data_Info);
            m_userBattleHash_C.insert(cIP,UBD);
            RefreshTableC();
            ui->tableView_C->sortByColumn(COLUMN_SCORE,Qt::DescendingOrder);
            break;
        case 4:
            hash_data_info_D.insert(cIP,data_Info);
            m_userBattleHash_D.insert(cIP,UBD);
            RefreshTableD();
            ui->tableView_D->sortByColumn(COLUMN_SCORE,Qt::DescendingOrder);
            break;
        default:
            break;
        }

    }
    if(json.contains("slotDisconnected"))
    {
        //断开连接去除用户信息显示

    }
}

void Battle_view::RefreshTableA()
{
    if(m_pTableModel_A->rowCount() > 0)
    {
        m_pTableModel_A->removeRows(0, m_pTableModel_A->rowCount());
    }

    QHashUserBattleIterator i = m_userBattleHash_A.begin();
    int row = 0;
    for(; i != m_userBattleHash_A.end(); ++i)
    {
        USER_BATTLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_A->insertRow(row, QModelIndex());
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_NO), row+1);
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_NAME), data.sName);
        m_pTableModel_A->setData(m_pTableModel_A->index(row,COLUMN_SCORE),data.score);
        // 设置全部列居中显示
        for(int j = 0; j <= COLUMN_SCORE; j++)
        {
            m_pTableModel_A->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }

}

void Battle_view::RefreshTableB()
{
    if(m_pTableModel_B->rowCount() > 0)
    {
        m_pTableModel_B->removeRows(0, m_pTableModel_B->rowCount());
    }

    QHashUserBattleIterator i = m_userBattleHash_B.begin();
    int row = 0;
    for(; i != m_userBattleHash_B.end(); ++i)
    {
        USER_BATTLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_B->insertRow(row, QModelIndex());
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_NO), row+1);
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_NAME), data.sName);
        m_pTableModel_B->setData(m_pTableModel_B->index(row,COLUMN_SCORE),data.score);
        // 设置全部列居中显示
        for(int j = 0; j <= COLUMN_SCORE; j++)
        {
            m_pTableModel_B->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}

void Battle_view::RefreshTableC()
{
    if(m_pTableModel_C->rowCount() > 0)
    {
        m_pTableModel_C->removeRows(0, m_pTableModel_C->rowCount());
    }

    QHashUserBattleIterator i = m_userBattleHash_C.begin();
    int row = 0;
    for(; i != m_userBattleHash_C.end(); ++i)
    {
        USER_BATTLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_C->insertRow(row, QModelIndex());
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_NO), row+1);
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_NAME), data.sName);
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_SCORE), data.score);
        // 设置全部列居中显示
        for(int j = 0; j <= COLUMN_SCORE; j++)
        {
            m_pTableModel_C->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}


void Battle_view::RefreshTableD()
{
    if(m_pTableModel_D->rowCount() > 0)
    {
        m_pTableModel_D->removeRows(0, m_pTableModel_D->rowCount());
    }

    QHashUserBattleIterator i = m_userBattleHash_D.begin();
    int row = 0;
    for(; i != m_userBattleHash_D.end(); ++i)
    {
        USER_BATTLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_D->insertRow(row, QModelIndex());
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_NO), row+1);
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_NAME), data.sName);
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_SCORE), data.score);
        // 设置全部列居中显示
        for(int j = 0; j <= COLUMN_SCORE; j++)
        {
            m_pTableModel_D->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}
void Battle_view::update_a_Data()
{
    QDateTime endTime=  QDateTime::currentDateTime();
    qint64 intervalTime = startTime.secsTo(endTime);
    int progress=intervalTime*100.0/NeedTime;
    if(progress>100)
        progress=100;
    ui->label->setText("进度："+QString::number(progress)+"%");
    if(intervalTime>=NeedTime)
        ui->pushButton_save->setVisible(true);
    QList<double>pa,pb;
    QList<double>pc,pd;
    QHashDataInfoIterator i = hash_data_info_A.begin();
    for(; i != hash_data_info_A.end(); ++i)
    {
        DATA_INFO data = i.value();
        if(data.PhysicalMentalHealth>0)
            pa.append(data.PhysicalMentalHealth);
    }
    double pmh_a=mean_List(pa);


    i = hash_data_info_B.begin();
    for(; i != hash_data_info_B.end(); ++i)
    {
        DATA_INFO data = i.value();
        if(data.PhysicalMentalHealth>0)
            pb.append(data.PhysicalMentalHealth);
    }
    double pmh_b=mean_List(pb);

    i = hash_data_info_C.begin();
    for(; i != hash_data_info_C.end(); ++i)
    {
        DATA_INFO data = i.value();
        if(data.PhysicalMentalHealth>0)
            pc.append(data.PhysicalMentalHealth);
    }
    double pmh_c=mean_List(pc);

    i = hash_data_info_D.begin();
    for(; i != hash_data_info_D.end(); ++i)
    {
        DATA_INFO data = i.value();
        if(data.PhysicalMentalHealth>0)
            pd.append(data.PhysicalMentalHealth);
    }
    double pmh_d=mean_List(pd);

    if(pmh_a>0 || pmh_b>0 || pmh_c>0 || pmh_d>0 ){
        cc.update_LineChart_Battle_LinesData(pmh_a,pmh_b,pmh_c,pmh_d);
        cc.update_ABCDHistBarData(pmh_a,pmh_b,pmh_c,pmh_d);
    }
    if(isAExit && pmh_a>0){
        PhysicalMentalHealth_list_A.append(pmh_a);
    }
    if(isBExit && pmh_b>0){
        PhysicalMentalHealth_list_B.append(pmh_b);
    }
    if(isCExit && pmh_c>0){
        PhysicalMentalHealth_list_C.append(pmh_c);
    }
    if(isDExit && pmh_d>0){
        PhysicalMentalHealth_list_D.append(pmh_d);
    }




}
double Battle_view::mean_List(QList<double>l)
{
    int len=l.size();
    if(len>0){
        double sum=0;
        for(int i=0;i<len;i++)
        {
            sum+=l.at(i);
        }
        return qRound(sum/len * 10) / 10;
    }else
    {
        return 0;
    }
}

void Battle_view::on_pushButton_save_clicked()
{
    QDateTime endTime=  QDateTime::currentDateTime();
    qint64 intervalTime = startTime.secsTo(endTime);
    if(intervalTime>NeedTime)
    {
        QString file=Constant::DataBasePath;
        Qt::HANDLE threadId = QThread::currentThreadId();
        QString threadIdStr = QString::number(reinterpret_cast<quintptr>(threadId));

        MySql *mySql = new MySql(file,threadIdStr);
        DataInfoBean dib;
        dib.TaskID=TaskID;

        dib.MeasureTime=endTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
        dib.TaskType="对抗模式";
        dib.Theme=Type;
        dib.Content=Scene;
        QJsonObject jsonObject;
        QJsonArray jsonArray_HR;
        QJsonArray jsonArray_Ox;
        QJsonArray jsonArray_SDP;
        QJsonArray jsonArray_UserID,jsonArray_UserName;
        QJsonArray jsonArray_PhysicalMentalHealth;
        QHashDataInfoIterator i = hash_data_info_A.begin();
        int row=0;//对抗人数
        int GroupNum=0;
        QString Group_A_Names,Group_B_Names,Group_C_Names,Group_D_Names;
        QList<ReportDataForSort> m_data_A,m_data_B,m_data_C,m_data_D;
        if(isAExit){
            GroupNum=GroupNum+1000;
            QJsonObject MyData_A;
            for(; i != hash_data_info_A.end(); ++i)
            {
                ReportDataForSort rdfs;
                DATA_INFO data = i.value();
                DATA_INFO utd=hash_data_info_A[data.IP];
                jsonArray_UserID.append(utd.UserID);//用户编号
                jsonArray_UserName.append(utd.UserName);//用户姓名
                rdfs.name=utd.UserName;
                rdfs.id=utd.UserID;
                jsonArray_HR.append(data.HR);
                jsonArray_Ox.append(data.Ox);
                jsonArray_SDP.append(data.SDP);
                jsonArray_PhysicalMentalHealth.append(data.PhysicalMentalHealth);
                rdfs.score=data.PhysicalMentalHealth;
                m_data_A.append(rdfs);

                QJsonArray jsonArray_hrvIndex;
                QJsonArray jsonArray_rr;

                for(int i=0;i<28;i++)
                {
                    jsonArray_hrvIndex.append(data.hrvindex[i]);
                }

                for(int i=0;i<data.rrValidLen;i++)
                {
                    jsonArray_rr.append(data.RR[i]);
                }
                MyData_A.insert("rr",jsonArray_rr);
                MyData_A.insert("hrvIndex",jsonArray_hrvIndex);
                MyData_A.insert("Score",data.PhysicalMentalHealth);
                MyData_A.insert("HR",data.HR);
                MyData_A.insert("Ox",data.Ox);
                MyData_A.insert("SDP",data.SDP);
                jsonObject.insert(utd.UserID,MyData_A);//每个人的测量结果数据
                row++;
            }

            std::sort(m_data_A.begin(),m_data_A.end(),compareData);

        }


        if(isBExit)
        {
            QJsonObject MyData_B;
            GroupNum=GroupNum+100;
            i = hash_data_info_B.begin();
            for(; i != hash_data_info_B.end(); ++i)
            {
                ReportDataForSort rdfs;
                DATA_INFO data = i.value();
                DATA_INFO utd=hash_data_info_B[data.IP];
                jsonArray_UserID.append(utd.UserID);//用户编号
                jsonArray_UserName.append(utd.UserName);//用户姓名
                rdfs.name=utd.UserName;
                rdfs.id=utd.UserID;
                jsonArray_HR.append(data.HR);
                jsonArray_Ox.append(data.Ox);

                jsonArray_SDP.append(data.SDP);
                jsonArray_PhysicalMentalHealth.append(data.PhysicalMentalHealth);
                m_data_B.append(rdfs);
                QJsonArray jsonArray_hrvIndex;
                QJsonArray jsonArray_rr;

                for(int i=0;i<28;i++)
                {
                    jsonArray_hrvIndex.append(data.hrvindex[i]);
                }

                for(int i=0;i<data.rrValidLen;i++)
                {
                    jsonArray_rr.append(data.RR[i]);
                }
                MyData_B.insert("rr",jsonArray_rr);
                MyData_B.insert("hrvIndex",jsonArray_hrvIndex);
                MyData_B.insert("Score",data.PhysicalMentalHealth);
                MyData_B.insert("HR",data.HR);
                MyData_B.insert("Ox",data.Ox);
                MyData_B.insert("SDP",data.SDP);
                jsonObject.insert(utd.UserID,MyData_B);//每个人的测量结果数据
                row++;
            }
            std::sort(m_data_B.begin(),m_data_B.end(),compareData);
        }


        if(isCExit)
        {
            QJsonObject MyData_C;
            GroupNum=GroupNum+10;
            i = hash_data_info_C.begin();
            for(; i != hash_data_info_C.end(); ++i)
            {
                ReportDataForSort rdfs;
                DATA_INFO data = i.value();
                DATA_INFO utd=hash_data_info_C[data.IP];
                jsonArray_UserID.append(utd.UserID);//用户编号
                jsonArray_UserName.append(utd.UserName);//用户姓名
                rdfs.name=utd.UserName;
                rdfs.id=utd.UserID;
                jsonArray_HR.append(data.HR);
                jsonArray_Ox.append(data.Ox);

                jsonArray_SDP.append(data.SDP);
                jsonArray_PhysicalMentalHealth.append(data.PhysicalMentalHealth);
                m_data_C.append(rdfs);
                QJsonArray jsonArray_hrvIndex;
                QJsonArray jsonArray_rr;

                for(int i=0;i<28;i++)
                {
                    jsonArray_hrvIndex.append(data.hrvindex[i]);
                }
                for(int i=0;i<data.rrValidLen;i++)
                {
                    jsonArray_rr.append(data.RR[i]);
                }
                MyData_C.insert("rr",jsonArray_rr);
                MyData_C.insert("hrvIndex",jsonArray_hrvIndex);
                MyData_C.insert("Score",data.PhysicalMentalHealth);
                MyData_C.insert("HR",data.HR);
                MyData_C.insert("Ox",data.Ox);
                MyData_C.insert("SDP",data.SDP);
                jsonObject.insert(utd.UserID,MyData_C);//每个人的测量结果数据
                row++;
            }
            std::sort(m_data_C.begin(),m_data_C.end(),compareData);
        }


        if(isDExit){
            QJsonObject MyData_D;
            GroupNum=GroupNum+1;
            i = hash_data_info_D.begin();
            for(; i != hash_data_info_D.end(); ++i)
            {
                ReportDataForSort rdfs;
                DATA_INFO data = i.value();
                DATA_INFO utd=hash_data_info_D[data.IP];
                jsonArray_UserID.append(utd.UserID);//用户编号
                jsonArray_UserName.append(utd.UserName);//用户姓名
                rdfs.name=utd.UserName;
                rdfs.id=utd.UserID;
                jsonArray_HR.append(data.HR);
                jsonArray_Ox.append(data.Ox);

                jsonArray_SDP.append(data.SDP);
                jsonArray_PhysicalMentalHealth.append(data.PhysicalMentalHealth);
                m_data_D.append(rdfs);
                QJsonArray jsonArray_hrvIndex;
                QJsonArray jsonArray_rr;

                for(int i=0;i<28;i++)
                {
                    jsonArray_hrvIndex.append(data.hrvindex[i]);
                }
                for(int i=0;i<data.rrValidLen;i++)
                {
                    jsonArray_rr.append(data.RR[i]);
                }
                MyData_D.insert("rr",jsonArray_rr);
                MyData_D.insert("hrvIndex",jsonArray_hrvIndex);
                MyData_D.insert("Score",data.PhysicalMentalHealth);
                MyData_D.insert("HR",data.HR);
                MyData_D.insert("Ox",data.Ox);
                MyData_D.insert("SDP",data.SDP);
                jsonObject.insert(utd.UserID,MyData_D);//每个人的测量结果数据
                row++;
            }
            std::sort(m_data_D.begin(),m_data_D.end(),compareData);
        }

        jsonObject.insert("Group_Number", row);//团体训练人数
        jsonObject.insert("Train_Type",Type);
        jsonObject.insert("Scene",Scene);
        jsonObject.insert("UseTime",(int)(intervalTime/60));//分钟
        jsonObject.insert("jsonArray_UserID",jsonArray_UserID);
        jsonObject.insert("jsonArray_UserName",jsonArray_UserName);
        jsonObject.insert("jsonArray_HR",jsonArray_HR);
        jsonObject.insert("jsonArray_Ox",jsonArray_Ox);

        jsonObject.insert("jsonArray_SDP",jsonArray_SDP);
        jsonObject.insert("jsonArray_PMH",jsonArray_PhysicalMentalHealth);



        QJsonArray jsonArray_PhysicalMentalHealth_list_A;
        QJsonArray jsonArray_PhysicalMentalHealth_list_B;
        QJsonArray jsonArray_PhysicalMentalHealth_list_C;
        QJsonArray jsonArray_PhysicalMentalHealth_list_D;


        if(isAExit){
            int A_Num=0;//A组人数
            i = hash_data_info_A.begin();
            for(; i != hash_data_info_A.end(); ++i)
            {
                A_Num++;
            }
            for(int k=0;k<PhysicalMentalHealth_list_A.size();k++)
            {
                jsonArray_PhysicalMentalHealth_list_A.append(PhysicalMentalHealth_list_A.at(k));
            }
            int Num_A=A_Num>3?3:A_Num;
            for(int i=0;i<Num_A;i++)
            {
                Group_A_Names+=m_data_A.at(i).name+";";
            }
        }


        if(isBExit)
        {
            i = hash_data_info_B.begin();
            int B_Num=0;//A组人数
            for(; i != hash_data_info_B.end(); ++i)
            {
                B_Num++;
            }
            for(int k=0;k<PhysicalMentalHealth_list_B.size();k++)
            {
                jsonArray_PhysicalMentalHealth_list_B.append(PhysicalMentalHealth_list_B.at(k));
            }
            int Num_B=B_Num>3?3:B_Num;
            for(int i=0;i<Num_B;i++)
            {
                Group_B_Names+=m_data_B.at(i).name+";";
            }
        }


        if(isCExit)
        {
            int C_Num=0;//A组人数
            i = hash_data_info_C.begin();
            for(; i != hash_data_info_C.end(); ++i)
            {
                C_Num++;
            }
            for(int k=0;k<PhysicalMentalHealth_list_C.size();k++)
            {
                jsonArray_PhysicalMentalHealth_list_C.append(PhysicalMentalHealth_list_C.at(k));
            }
            int Num_C=C_Num>3?3:C_Num;
            for(int i=0;i<Num_C;i++)
            {
                Group_C_Names+=m_data_C.at(i).name+";";
            }
        }


        if(isDExit){
            int D_Num=0;//A组人数
            i = hash_data_info_D.begin();
            for(; i != hash_data_info_D.end(); ++i)
            {
                D_Num++;
            }
            for(int k=0;k<PhysicalMentalHealth_list_D.size();k++)
            {
                jsonArray_PhysicalMentalHealth_list_D.append(PhysicalMentalHealth_list_D.at(k));
            }
            int Num_D=D_Num>3?3:D_Num;
            for(int i=0;i<Num_D;i++)
            {
                Group_D_Names+=m_data_D.at(i).name+";";
            }
        }

        jsonObject.insert("Group_A_PhysicalMentalHealth_Trend",jsonArray_PhysicalMentalHealth_list_A);
        jsonObject.insert("Group_B_PhysicalMentalHealth_Trend",jsonArray_PhysicalMentalHealth_list_B);
        jsonObject.insert("Group_C_PhysicalMentalHealth_Trend",jsonArray_PhysicalMentalHealth_list_C);
        jsonObject.insert("Group_D_PhysicalMentalHealth_Trend",jsonArray_PhysicalMentalHealth_list_D);
        jsonObject.insert("Group_A_Names",Group_A_Names);
        jsonObject.insert("Group_B_Names",Group_B_Names);
        jsonObject.insert("Group_C_Names",Group_C_Names);
        jsonObject.insert("Group_D_Names",Group_D_Names);
        jsonObject.insert("GroutEixstNum",GroupNum);

        dib.GroupNum=GroupNum;
        dib.TaskResult=QString(QJsonDocument(jsonObject).toJson());
        mySql->MyDataInsert(dib);
        delete mySql;
        this->close();
    }
}

